//
//  EJSGoodsEvaluateListViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2018/1/2.
//  Copyright © 2018年 lifayu. All rights reserved.
//

#import "EJSGoodsEvaluateListViewController.h"

#import "EJSGoodsEvaluateTableViewCell.h"
#import "EJSEvaluateTableViewCell.h"

#import "EJSEvaluateRequest.h"

@interface EJSGoodsEvaluateListViewController (){
    NSInteger _currentPage;
    NSString *_totalEvaluate;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation EJSGoodsEvaluateListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"评论列表";
    [self getGoodsEvaluateList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)configOwnViews{
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSGoodsEvaluateTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSGoodsEvaluateTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSEvaluateTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSEvaluateTableViewCell"];
    
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.estimatedRowHeight = 50;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage = 1;
        [self getGoodsEvaluateList];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _currentPage ++;
        [self getGoodsEvaluateList];
    }];
}

- (void)getGoodsEvaluateList{
    EJSEvaluateRequest *req = [[EJSEvaluateRequest alloc] init];
    req.spuId = self.spuId;
    req.page = @(_currentPage).stringValue;
    req.pageLimit = @"20";
    FlyWeakSelf
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        
        EJSEvaluateListModel *listModel = response.data;
        if(listModel){
            if(listModel.totalPages.integerValue == listModel.page.integerValue){
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            if(_currentPage == 1){
                weakSelf.dataSource = [NSMutableArray arrayWithArray:listModel.list];
            }else{
                [weakSelf.dataSource addObjectsFromArray:listModel.list];
            }
        }
        _totalEvaluate = listModel.total;
        [weakSelf.tableView reloadData];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}
#pragma mark - UITableViewDataSource Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0){
        EJSGoodsEvaluateTableViewCell *goodsEvaluateCell = [tableView dequeueReusableCellWithIdentifier:@"EJSGoodsEvaluateTableViewCell" forIndexPath:indexPath];
        goodsEvaluateCell.goodsEvaluateCountLabel.text = [NSString stringWithFormat:@"用户评价（%i）",_totalEvaluate.intValue];
        return goodsEvaluateCell;
    }else{
        EJSEvaluateTableViewCell *evaluateCell = [tableView dequeueReusableCellWithIdentifier:@"EJSEvaluateTableViewCell" forIndexPath:indexPath];
        [evaluateCell setEvaluateModel:self.dataSource[indexPath.row - 1]];
        return evaluateCell;
    }
    return [UITableViewCell new];
}

@end
